<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="店铺" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">
              <a-input v-model="form.shopId" placeholder="请输入店铺id"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评论人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <a-input-number v-model="form.userId" placeholder="请输入用户号" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评论内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comment">
              <a-textarea rows="5" v-decorator="['comment',{trigger:'input'}]"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="评分星级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stars">
              <a-input-number v-model="form.stars" placeholder="请输入1-5的五个星星等级" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" >
<!--              <a-input v-model="form.imgUrls" placeholder="请输入图片链接以逗号连接，最多9张"></a-input>-->
              <j-image-upload isMultiple v-decorator="['imgUrls']"></j-image-upload>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="商家回复" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopComment">-->
<!--              <a-input v-model="form.shopComment" placeholder="请输入商家回复"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="回复时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="answerTime">-->
<!--              <j-date placeholder="请选择回复时间" v-model="form.answerTime" :trigger-change="true" style="width: 100%"/>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="热门（0，正常 1，热门）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isHot">-->
<!--              <a-input-number v-model="form.isHot" placeholder="请输入热门（0，正常 1，热门）" style="width: 100%"/>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sequence">-->
<!--              <a-input-number v-model="form.sequence" placeholder="请输入排序" style="width: 100%"/>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="状态（1启用，0不启用）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">-->
<!--              <a-input v-model="form.status" placeholder="请输入状态（1启用，0不启用）"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="删除状态（0，正常，1已删除）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">-->
<!--              <a-input v-model="form.delFlag" placeholder="请输入删除状态（0，正常，1已删除）"></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'

  export default {
    name: 'SmsShopCommentForm',
    components: {
      JFormContainer,
      JDate,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: {
            shopId:'',
            userId:'',
            comment:'',
            stars:'',
            imgUrls:[],
            shopComment:'',
            answerTime:'',
            isHot:'',
            sequence:'',
            status:'',
            delFlag:'',
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        rules: {
          comment:  [
              { required: true, message: '请输入评论内容!'},
            ],
          isHot:  [
              { required: true, message: '请输入热门（0，正常 1，热门）!'},
            ],
          status:  [
              { required: true, message: '请输入状态（1启用，0不启用）!'},
            ],
          delFlag:  [
              { required: true, message: '请输入删除状态（0，正常，1已删除）!'},
            ],
        },
        url: {
          add: "/shop/comment/add",
          edit: "/shop/comment/edit",
          queryById: "/shop/comment/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({
            shopId:'',
            userId:'',
            comment:'',
            stars:'',
            imgUrls:[],
            shopComment:'',
            answerTime:'',
            isHot:'',
            sequence:'',
            status:'',
            delFlag:'',
        })
      },
      edit (record) {
        this.form = Object.assign({}, record)
        this.visible = true
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.form.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign({}, this.form)
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form = row
      },
    }
  }
</script>
